import { useHttp } from './useHttp'

export interface selectReq {
  serviceName: string
  colNames: string[]
  page?: object
  use_type?: string
  query_source?: string
  condition?: any[]
  relation_condition?: object
  vpage_no?: string
  order?: any[]
}
export interface selectResp {
  data: any[]
  page: {
    total: number
  }
  state: 'SUCCESS' | 'FAIL'
}
export function getRequestUrl(service: string, type = 'select') {
  const config = useRuntimeConfig()
  const baseApp = config.public.baseApp
  const baseApi = config.public.baseApi
  return `${baseApi}/${baseApp}/${type}/${service}`
}
export function getRequestUrlSearch(service: string, type = 'select') {
  const config = useRuntimeConfig()
  const baseApp = config.public.baseApp
  const baseApi = config.public.baseApi
  return `${baseApi}/${baseApp}/${service}`
}

// 构建通用操作参数 适用于add\update\delete服务
export function buildCommonOperateBody(service: string, type: 'add' | 'update' | 'delete', data: object, id?: number) {
  const req = [
    {
      serviceName: service,
      data: [data || {}], // data：新增、修改的时候提交的数据
      // 编辑、删除的时候要传id
      condition: ['update', 'delete'].includes(type) ? [{ colName: 'id', value: id || data?.id, ruleType: 'eq' }] : [],
    },
  ]
  return req
}

// 构建通用查询参数---分页
export function buildCommonSelectBody(service = '', reqBody = {}): selectReq {
  const req = {
    serviceName: service,
    colNames: reqBody.colNames || ['*'],
    page: { pageNo: 1, rownumber: 10 },
    use_type: 'list',
    query_source: 'list_page',
    ...reqBody,
  }
  return req
}
// 构建通用查询参数--树
export function buildCommonSelectBodyTree(service = '', reqBody = {}): selectReq {
  const req = {
    serviceName: service,
    colNames: ['*'],
    treeData: true,
    ...reqBody,
  }
  return req
}
// 构建通用查询参数--搜索
export function buildCommonSelectBodySearch(service = '', reqBody = {}): selectReq {
  const req = {
    // serviceName: service,
    // colNames: ['*'],
    ...reqBody,
  }
  return req
}
// 健康陕西列表
export function getHealthSX(reqBody = {}) {
  const service = 'srvjk_healthy_data_publish_jksx_v_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 首页-头条资讯
export function getTopNews(reqBody = {}) {
  const service = 'srvjk_healthy_data_publish_tdzx_v_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 首页-专家库
export function getExpertList(reqBody = {}) {
  const service = 'srvjk_expert_info_web_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 首页-健康西安
export function getjksxxdList(reqBody = {}) {
  const service = 'srvjk_healthy_data_publish_jksxxd_v_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 首页-百姓健康系列栏目- 《陕西老年健康报》专版详情
export function getbxjkxllmDetail(reqBody = {}) {
  const service = 'srvjk_healthy_data_publish_sxlnjkb_v_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 首页-百姓健康系列栏目-
export function getbxjkxllm(reqBody = {}) {
  const service = 'srvjk_healthy_data_publish_bxjkxllm_v_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 首页-百姓健康系列栏目- 《陕西老年健康报》专版
export function getbxjkxllmList(reqBody = {}) {
  const service = 'srvjk_senile_health_news_web_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 首页-百姓健康系列栏目- 三秦百姓健康杂志
export function getsqbxjkList(reqBody = {}) {
  const service = 'srvjk_healthy_data_publish_sqbxjk_v_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 首页-百姓健康系列栏目-陕西日报详情
export function getsxrbDetail(reqBody = {}) {
  const service = 'srvjk_healthy_data_publish_sxrb_v_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 首页-百姓健康系列栏目-陕西日报
export function getsxrbList(reqBody = {}) {
  const service = 'srvjk_shanxi_daily_web_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 首页-百姓健康系列栏目-广播
export function getgblmList(reqBody = {}) {
  const service = 'srvjk_healthy_data_publish_gblm_v_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 首页-百姓健康系列栏目-电视栏目
export function getdslmList(reqBody = {}) {
  const service = 'srvjk_healthy_data_publish_dslm_v_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 首页-科普资料
export function getkpzlList(reqBody = {}) {
  const service = 'srvjk_healthy_data_publish_kpzl_v_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 获奖作品发布web
export function getPrizeWinningdata(reqBody = {}) {
  const service = 'srvjk_healthy_data_publish_hjzp_v_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 首页-宣传活动
export function getxchdList(reqBody = {}) {
  const service = 'srvjk_healthy_data_publish_xchd_v_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 首页-健康知识
export function getHealthKL(reqBody = {}) {
  const service = 'srvjk_healthy_data_publish_jkzs_v_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 分类管理查询
export function getCategoryList(reqBody = {}) {
  const service = 'srvjk_classify_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBodyTree(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}

// 职业健康作品征集大赛数据发布web
export function getHealthworks(reqBody = {}) {
  const service = 'srvjk_healthy_data_publish_xchd_zyjkzp_v_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 用户收藏文章增加
export function addCollectArticle(reqBody = []) {
  const service = 'srvjk_collect_add'
  const url = getRequestUrl(service, 'add')
  const req = buildCommonOperateBody(service, 'add', { target_id: reqBody[0] })
  // console.log('req',req);

  return useHttp.post(url, req, { useToken: true }).then((res) => {
    if (res.state === 'SUCCESS') {
      const { getCollectArticleList } = useUserStore()
      getCollectArticleList()
    }
    else if (res.resultMessage && res.resultCode !== '0011') {
      ElMessage.error(res.resultMessage)
    }
    return res
  })
}
// 用户收藏文章删除
export function deleteCollectArticle(reqBody = []) {
  const service = 'srvjk_collect_delete'
  const url = getRequestUrl(service, 'delete')
  const req = buildCommonOperateBody(service, 'delete', { id: reqBody[0] })
  return useHttp.post(url, req, { useToken: true }).then((res) => {
    if (res.state === 'SUCCESS') {
      const { getCollectArticleList } = useUserStore()
      getCollectArticleList()
      ElMessage.success('取消收藏成功')
    }
    else if (res.resultMessage) {
      ElMessage.error(res.resultMessage)
    }
  })
}
// 区划管理查询
export function getAreaList(reqBody = {}) {
  const service = 'srvjk_area_info_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 机构信息前端查询
export function getOrgList(reqBody = {}) {
  const service = 'srvjk_organ_info_web_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 社区活动数据发布web
export function getActData(reqBody = {}) {
  const service = 'srvjk_healthy_data_publish_xchd_sqhd_v_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 知汛防汛数据发布web
export function getFloodListData(reqBody = {}) {
  const service = 'srvjk_healthy_data_publish_xchd_zxfx_v_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 卫生宣传日数据发布web
export function getHealthData(reqBody = {}) {
  const service = 'srvjk_healthy_data_publish_xchd_wsxcr_v_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 论坛讲座数据发布web
export function getLectureData(reqBody = {}) {
  const service = 'srvjk_healthy_data_publish_xchd_ltjz_v_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 在线直播数据发布web
export function getLiveData(reqBody = {}) {
  const service = 'srvjk_healthy_data_publish_xchd_zxzb_v_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 知汛防汛数据发布web
export function getFloodData(reqBody = {}) {
  const service = 'srvjk_healthy_data_publish_kpzl_v_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}

export function getShenTai(reqBody = {}) {
  const service = 'srvdfz_biology_web_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}

// 健康陕西行动知行大赛数据发布web
export function getHealthBigData(reqBody = {}) {
  const service = 'srvjk_healthy_data_publish_xchd_zxds_v_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 专家作品信息发布查询web
export function getExpertWorkInfo(reqBody = {}) {
  const service = 'srvjk_healthy_data_publish_zjzp_v_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 查询web
export function searchList(reqBody = {}) {
  const service = 'search'
  const url = getRequestUrlSearch(service)
  const req = buildCommonSelectBodySearch(service, reqBody)
  if (!Array.isArray(req.sscope)) {
    if (req.sscope) {
      req.sscope = [req.sscope]
    }
    else {
      req.sscope = []
    }
  }
  if (!Array.isArray(req.fclass)) {
    if (req.fclass) {
      req.fclass = [req.fclass]
    }
    else {
      req.fclass = []
    }
  }
  return useHttp.post(url, req) as Promise<selectResp>
}
// 字典
export function dictList(reqBody = {}) {
  const service = 'srvsys_dict_jk_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 字典
export function areaListUrl(reqBody = {}) {
  const service = 'srvjk_area_info_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 问卷活动查询
export function getSurveyActivity(reqBody = {}) {
  const service = 'srvdaq_activity_cfg_wj_select'
  let url = getRequestUrl(service)
  url = url.replace('hsprl', 'daq')
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 竞答活动查询
export function getCompetitionActivity(reqBody = {}) {
  const service = 'srvdaq_activity_cfg_jd_select'
  let url = getRequestUrl(service)
  url = url.replace('hsprl', 'daq')
  const req = buildCommonSelectBody(service, reqBody)
  console.log(url, req)

  return useHttp.post(url, req) as Promise<selectResp>
}

// 查找用户已收藏文章
export function getCollectArticle(reqBody = {}) {
  const service = 'srvjk_collect_my_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 在线直播查询
export function getOnlineLive(reqBody = {}) {
  const service = 'srvjk_stream_info_web_select'
  // const service = 'srvjk_stream_info_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 在线直播查询
export function getSearchDetail(reqBody = {}) {
  const service = 'srvjk_healthy_data_publish_v_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 中医科普
export function zyjkkpList(reqBody = {}) {
  const service = 'srvjk_healthy_data_publish_zyjkkp_v_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 专家咨询聊天记录
export function onlieList(reqBody = {}) {
  const service = 'srvjk_online_question_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 专家咨询新增
export function onlieAdd(reqBody = {}) {
  const service = 'srvjk_online_question_add'
  const url = getRequestUrl(service, 'add')
  const req = buildCommonOperateBody(service, 'add', reqBody)
  // console.log('req',req);

  return useHttp.post(url, req, { useToken: true }).then((res) => {
    if (res.state === 'SUCCESS') {
      const { getCollectArticleList } = useUserStore()
      getCollectArticleList()
    }
    else if (res.resultMessage) {
      ElMessage.error(res.resultMessage)
    }
    return res
  })
  // const service = 'srvjk_online_question_add'
  // const url = getRequestUrl(service)
  // const req = buildCommonSelectBody(service, reqBody)
  // return useHttp.post(url, req) as Promise<selectResp>
}
// 专家咨询更新
export function onlieUpdata(reqBody = {}) {
  const service = 'srvjk_online_question_update'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 专家咨询模板
export function onlieSelect(reqBody = {}) {
  const service = 'srvjk_consultsy_mode_onlie_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 专家作品
export function expertDataList(reqBody = {}) {
  const service = 'srvjk_healthy_data_publish_zjzp_v_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 查询相关评论区
export function getCommentList(reqBody = {}) {
  const service = 'srvjk_comment_web_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 发布评论
export function sendComment(reqBody = {}) {
  const service = 'srvjk_comment_add'
  const url = getRequestUrl(service, 'add')
  const req = buildCommonOperateBody(service, 'add', reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
// 用户关注医师
export function userFollowDoctor(reqBody = {}) {
  const service = 'srvjk_follow_add'
  const url = getRequestUrl(service, 'add')
  // const url = getRequestUrl(service)
  // const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, reqBody) as Promise<selectResp>
}
// 用户取消关注医师
export function userUnFollowDoctor(reqBody = {}) {
  const service = 'srvjk_follow_delete'
  const url = getRequestUrl(service, 'delete')
  // const req = buildCommonOperateBody(service, 'delete', reqBody)
  // const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, reqBody) as Promise<selectResp>
}
// 获取用户关注医师
export function userFollowDoctorSelect(reqBody = {}) {
  const service = 'srvjk_follow_my_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
/**
 * @description: 上传稿件
 * @param data 提交数据
 * @returns 
 */
export function EmailUpload(data) {
  const service = 'srvdfz_collect_web_add'
  const body = {
    'phone': data.phone,
    'email': data.email,
    'people':data.name,
    'fujian':data.fileNo,
    'title':data.title,

  }
  return addComment(service, body)
}


export function getManuscriptList(reqBody = {}) {
  const service = 'srvdfz_collect_web_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>

}

export function getCollectionList(reqBody = {}) {
  const service = 'srvjk_collect_my_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>

}

export function getOrganList(reqBody = {}) {
  const service = 'srvjk_organ_info22_select'
  const url = getRequestUrl(service)
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>

}






export function addComment(service:string,body={},condition=[]) {
  let config=useRuntimeConfig().public
  let url = `${config.baseApi}/${config.baseApp}/operate/${service}`
  let data=[{
    "serviceName": service,
    "condition": condition,
    "data": [
      body
    ]
  }]
  return useHttp.post(url, data) as Promise<selectResp>
  }

// 查找书籍目录
export function getBookCatalogList(book_no: string) {
  if (!book_no)
    return { state: 'FAILED' }
  const service = 'srvjk_contents_web_select'
  const url = getRequestUrl(service)
  const reqBody = {
    condition: [{
      colName: 'book_no',
      ruleType: 'eq',
      value: book_no,
    }],
  }
  const req = buildCommonSelectBody(service, reqBody)
  return useHttp.post(url, req) as Promise<selectResp>
}
